{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "85662644",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-24T13:17:04.814792Z",
     "start_time": "2023-11-24T13:17:03.320380Z"
    }
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "from torch import nn"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f4064f5-9309-476e-be5e-99a09aed8243",
   "metadata": {},
   "source": [
    "### flush"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d21b51e7-565e-489d-a114-9cb4925bd693",
   "metadata": {},
   "source": [
    "```\n",
    "def flush_model(self):\n",
    "    # delete llm from gpu to save GPU memory\n",
    "    if self.model is not None:\n",
    "        self.model = None\n",
    "    gc.collect()\n",
    "    torch.cuda.empty_cache()\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21f9626b-e5d0-432f-a2aa-510e7d655ad8",
   "metadata": {},
   "source": [
    "### parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b0c4f453",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-24T13:16:53.035351Z",
     "start_time": "2023-11-24T13:16:53.029917Z"
    }
   },
   "outputs": [],
   "source": [
    "def num_parameters(model):\n",
    "    return sum(p.numel() for p in model.parameters() if p.requires_grad)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "774de0cc",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-24T13:17:12.603280Z",
     "start_time": "2023-11-24T13:17:12.596196Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "220"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m = nn.Linear(10, 20)\n",
    "num_parameters(m)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
